<template>
  <a-modal
    :title="(id ? isLook ?'查看':'编辑':'新增')+'招标需求计划'"
    :width="1010"
    :visible="visible"
    style="text-align:center"
    @cancel="handleCancel"
  >
    <div v-loading="loading">
      <a-form-model
        ref="form"
        :model="queryParam"
        style="transform:translateX(25%)"
        :label-col="{lg: {span: 5}, sm: {span:5}}"
        :wrapper-col="{lg: {span: 17}, sm: {span: 17} }"
        :rules="rules"
      >
        <a-row :gutter="24">
            <a-col :md="12" :sm="12">
              <a-form-model-item label="所属单位"  :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }" :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }" class="requiredFormItem">
                <org-tree-select v-model="queryParam.projectId"  :disabled="isLook"  isAutoSelectFirst checkStrictly @select="selectOrg"></org-tree-select>
              </a-form-model-item>
            </a-col>
          <!-- <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="所属分公司"
              prop="branchCompanyId"
            >
              <SelectSubCompany
                v-model="queryParam.branchCompanyId"
                :disabled="isLook"
                placeholder="请选择"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="项目名称"
              prop="projectId"
            >
              <SelectProject
                v-if="visible"
                v-model="queryParam.projectId"
                :disabled="isLook"
                placeholder="请选择"
                :form="queryParam"
                :is-clear="false"
                :is-edit="true"
                key-value="branchCompanyId"
              />
            </a-form-model-item>
          </a-col> -->
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="分包工程内容"
              prop="biddingContent"
            >
              <BaseInput
                v-model.trim="queryParam.biddingContent"
                :disabled="isLook"
                placeholder="请输入分包工程名称"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="计划进场时间"
              prop="planStartTime"
            >
              <a-date-picker
                v-model="queryParam.planStartTime"
                :disabled="isLook"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择项目进场日期"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="计划队伍数量"
              prop="plannedTeamsNum"
            >
              <a-input-number
                v-model.number="queryParam.plannedTeamsNum"
                :disabled="isLook"
                 :min="0"
                 decimalSeparator="0"
                placeholder="请输入计划队伍数量"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item label="附件">
              <b-upload
                :multiple="false"
                :maxUploadCount="1"
                :disabled="isLook"
                upload-type="single"
                business-code="LCM16"
                :file-list.sync="fileInfoVO"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col
            :md="12"
            :sm="12"
          >
            <a-form-model-item
              label="备注"
              prop="remark"
            >
              <BaseTextarea
                v-model.trim="queryParam.remark"
                :show-limit-tip="!isLook"
                :disabled="isLook"
                :auto-size="{ minRows: 3, maxRows: 5 }"
                placeholder="请输入备注"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <template slot="footer">
      <a-button @click="handleCancel">
        取消
      </a-button>
      <template v-if="!isLook">
        <a-button
          :loading="loading"
          type="primary"
          @click="handleOk(queryParam,'DRAFT')"
        >
          保存
        </a-button>
        <a-button
          :loading="loading"
          type="primary"
          @click="handleOk(queryParam,'PAST')"
        >
          提交
        </a-button>
      </template>
    </template>
  </a-modal>
</template>

<script>
import { getBranchBiddingdetails, addBranchBiddingList, editBranchBiddingList } from '@/api/legal'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 分公司
export default {
  name: 'a' + Date.now(),
  components: {
    SelectSubCompany,
    SelectProject
  },
  props: {
    isLook: {
      type: Boolean,
      required: false
    },
    visible: {
      type: Boolean,
      required: true
    },
    id: {
      type: String,
      default: () => undefined
    }
  },
  data() {
    return {
      loading: false,
      queryParam: { fileInfoVO: {}, projectId: '' },
      fileInfoVO: [],
      rules: {
        branchCompanyId: [{ required: true, message: '请选择分公司名称', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
        biddingContent: [{ required: true, message: '请输入分包工程内容', trigger: 'change' }],
        planStartTime: [{ required: true, message: '请输入项目进场日期', trigger: 'change' }],
        plannedTeamsNum: [{ required: true, message: '请输入计划队伍数量', trigger: 'blur' }]
      },
      branchCompanyList: [],
      projectList: []
    }
  },
  mounted () {
    console.log("===3333545")
  },
  methods: {
    //选择单位
    selectOrg(val) {
      if (val && val.length) {
        this.queryParam.branchCompanyId = val[val.length - 2].orgId;
      }
    },
    handleCancel() {
      this.$emit('update:visible', false)
      this.$refs.form.clearValidate()
      this.queryParam = { fileInfoVO: {} }
    },
    handleOk(subQueryParam, status) {
      const form = this.$refs.form
      form.validate((valid) => {
        if (!valid) {
          return this.$message.info('信息不完整')
        }
        this.queryParam.fileInfoVO = this.fileInfoVO[0] || []
        subQueryParam.auditStatus = status
        if(subQueryParam.fileInfoVO && subQueryParam.fileInfoVO.length==0){
          subQueryParam.fileInfoVO = null;
        }
        this.loading = true
        if (this.id) {
          editBranchBiddingList(subQueryParam).then((res) => {
            if (res.code === 200) {
              this.$message.success('修改成功')
              this.$emit('ok')
              this.handleCancel()
            } else {
              this.$message.error('提交失败，请从新提交！')
            }
            this.loading = false
          })
        } else {
          subQueryParam.id = this.id
          addBranchBiddingList(subQueryParam).then((res) => {
            if (res.code === 200) {
              this.$message.success('新增成功')
              this.$emit('ok')
              this.handleCancel()
            } else {
              this.$message.error('提交失败，请从新提交！')
            }
            this.loading = false
          })
        }
      })
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        if (this.id) {
          this.loading = true
          getBranchBiddingdetails({ id: this.id })
            .then((res) => {
              this.loading = false
              this.queryParam = res.data
              this.fileInfoVO = [this.queryParam.fileInfoVO]
              this.$console(res.data)
            })
            .catch((res) => {
              this.loading = false
            })
        }
      } else {
        this.handleCancel()
      }
    }
  }
}
</script>
